/**
 * 
 */
package it.rainbowbreeze.polygen.logic;

import android.text.TextUtils;
import it.rainbowbreeze.polygen.common.LogFacility;

/**
 * @author rainbowbreeze
 *
 */
public class MessageParser {
    private static final String LOG_HASH = MessageParser.class.getSimpleName(); 
    private final LogFacility mLogFacility;
    
    public MessageParser(LogFacility logFacility) {
        mLogFacility = logFacility;
    }
    
    
    public String parse(String rawMessage)  {
        if (TextUtils.isEmpty(rawMessage)) return null;
        
        String[] rows = rawMessage.split("\n");
        if (rows.length < 3) {
            mLogFacility.v(LOG_HASH, "Cannot obtain raw message");
            return null;
        }
        String message = rows[2];
        message = message.replace("document.write(\"", "");
        message = message.replace("\");", "");
        message = message.replace("\\\"", "\"");
        message = message.replace("<br>", "\n");
        message = message.replaceAll("<[^>]+>", "");
//        message = removeSymbolWithSpaces(message, "<b>");
//        message = removeSymbolWithSpaces(message, "<i>");
        message = message.replace("\\'", "'");
        message = message.replace("&#186;", "a");
        mLogFacility.v(LOG_HASH, "message is: " + message);
        return message;
    }
    
    private String removeSymbolWithSpaces(String source, String symbol) {
        String message;
        message = source.replace(" " + symbol + " ", "");
        message = message.replace(" " + symbol, "");
        message = message.replace(symbol + " ", "");
        message = message.replace(symbol, "");
        return message;
    }
}
